<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contact extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('Contact_model');
		$this->load->model('Produk_model');
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->helper('bantuan_helper');
	}

	function index()
	{
		$this->form_validation->set_rules('nama_kontak', 'Name', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
		$this->form_validation->set_rules('pesan', 'Message', 'required');
		$this->form_validation->set_rules('number', 'captcha number', 'required');
		$this->form_validation->set_rules('subject', 'Subject', 'required');

		if ($this->form_validation->run() == FALSE):
			$data['title'] = 'Kontak Kami';
			$data['ses_id'] = $this->session->userdata('session_id');
			$data['main_view'] = 'contact';
			$data['cumb'] = anchor('produk','<strong>Home</strong>').' &raquo; Contact';	
			$this->load->view('index',$data);
		else:
			session_start();

			$key=substr($_SESSION['key'],0,6);
			$number = $this->input->post('number');

			if($number!=$key):
				$this->session->set_flashdata('message_type', 'Validation string not valid! Please try again!');
				redirect('contact');
			else:
				$message = array('subject'=>$this->input->post('subject'),
								 'message'=>$this->input->post('pesan'));

				$data = array('nama_kontak' => $this->input->post('nama_kontak'),
							  'email' => $this->input->post('email'),
							  'pesan' => serialize($message),
							  'date'=>date('Y-m-d h:i:s'));
				$this->Contact_model->addContact($data);
				$this->session->set_flashdata('message_type', 'Terima kasih atas partisipasi anda');
				redirect('contact');
			endif;
		endif;
	}

}